package com.alibaba.citrus.cr.price.ability.functions.factory.activity;

import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.cr.price.ability.functions.constant.ActivityOrderTagEnum;
import com.alibaba.citrus.cr.price.ability.functions.entity.activity.ActivityEntity;
import com.alibaba.citrus.cr.price.ability.functions.entity.activity.ActivityTermEntity;
import com.alibaba.citrus.cr.price.ability.functions.entity.activity.ActivityTitleEntity;
import com.alibaba.citrus.cr.price.ability.functions.entity.activity.ActivityTitleExtEntity;
import com.alibaba.citrus.cr.price.ability.functions.entity.activity.ActivityUseRecordRelationEntity;
import com.alibaba.cz.base.tool.log.Log;
import com.epoch.app.crpricecenter.dto.request.ActivityInfoRequest;
import com.epoch.app.crpricecenter.dto.request.ParticipateActivityInfoRequest;
import com.epoch.app.crpricecenter.model.enums.ActivityTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: qiaojiange
 * @create: 2021/10/17
 * @Description 活动抬头工厂类
 */
public class ActivityFactory {

    private static Log log = Log.getLogger(ActivityFactory.class);
    private final static ArrayList<String> replenishList = Lists.newArrayList(ActivityTypeEnum.REPLENISH_LIST.getValue().split(","));

    /**
     * 创建活动实体, 参加活动使用
     *
     * @param request 请求参数
     * @return
     */
    public static ActivityEntity create(ParticipateActivityInfoRequest request) {
        ActivityTitleEntity titleEntity = ActivityTitleFactory.create(request.getActivityId());
        List<ActivityTermEntity> termEntityList = ActivityTermFactory.create(request, titleEntity);
        ActivityTitleExtEntity titleExtEntity = ActivityTitleExtFactory.create(titleEntity);
        ActivityUseRecordRelationEntity useEntity = ActivityUseRecordRelationFactory.create(request);
        return ActivityEntity.of(titleEntity, termEntityList, titleExtEntity, useEntity);
    }

    /**
     * 创建活动实体，查询活动使用
     *
     * @param request 入参
     * @return
     */
    public static List<ActivityEntity> create(ActivityInfoRequest request) {

        // 一个预报订单会不会命中两个活动抬头,会有的，出现多个活动抬头，就是活动冲突
        //1. 查询活动抬头表
        //2. 查询活动项目表
        //3. 根据分组标识，判断是否查询活动抬头扩展表
        //4. 构造活动实体
        List<ActivityTitleEntity> titleEntityList = ActivityTitleFactory.create(request);
        if (CollectionUtils.isEmpty(titleEntityList)) {
            return new ArrayList<>();
        }

        //补货类型的活动的处理
        if (ActivityOrderTagEnum.REPLENISH_ORDER.getCode().equals(request.getOrderType())) {
            titleEntityList = titleEntityList.stream().filter(e ->
                    replenishList.contains(e.getActivityTitleSDO().getActivityFacadeCode())).collect(Collectors.toList());
        } else {
            //其他活动处理
            titleEntityList = titleEntityList.stream().filter(e ->
                    !replenishList.contains(
                            e.getActivityTitleSDO().getActivityFacadeCode())).collect(Collectors.toList());
        }

        if (CollectionUtils.isEmpty(titleEntityList)) {
            return new ArrayList<>();
        }

        ArrayList<ActivityEntity> result = new ArrayList<>();
        titleEntityList.forEach(titleEntity -> {
            List<ActivityTermEntity> termEntityList = new ArrayList<>();
            ActivityTitleExtEntity titleExtEntity = null;
            if (titleEntity.hasTitleExt()) {
                titleExtEntity = ActivityTitleExtFactory.create(titleEntity);
            }

            termEntityList = ActivityTermFactory.create(titleEntity, request);

//            过滤掉活动项目剩余数量0且剩余金额为0的活动项目
            List<ActivityTermEntity> filterTermEntityList = filterTermEntityList(termEntityList,titleEntity.obtainControlByGroupFlag());
            if (CollectionUtils.isNotEmpty(filterTermEntityList)) {
                result.add(ActivityEntity.of(titleEntity, filterTermEntityList, titleExtEntity, null));
            }
        });
        return result;
    }

    /**
     * 过滤掉活动项目剩余数量0且剩余金额为0的活动项目
     * @param termEntityList 活动term 项目列表
     * @return
     */
    @NotNull
    private static List<ActivityTermEntity> filterTermEntityList(List<ActivityTermEntity> termEntityList,String flag) {
        List<ActivityTermEntity> filterTermEntityList = new ArrayList<>();
        if (CollectionUtils.isEmpty(termEntityList)) {
            return filterTermEntityList;
        }
        //分组管控标识为'X'时，不对Term里的活动剩余费用和货品剩余数量做校验
        if("X".equals(flag)){
            return termEntityList;
        }

        for (ActivityTermEntity entity : termEntityList) {
            if (entity.obtainActivityRemainingQuantity() <= 0 && entity.obtainActivityRemainingFee() <= 0) {
                log.error("活动项目剩余数量0且剩余金额为0，不做展示。termEntity={}", entity);
                continue;
            } else {
                filterTermEntityList.add(entity);
            }
        }
        return filterTermEntityList;
    }
}
